<div class="tabbable tabs-left">
    <ul class="nav nav-tabs">
        <li><h5><?php echo $this->translate('商品'); ?></h5></li>
        <li class="active"><a data-toggle="tab" href="#tag_a"><?php echo $this->translate('基本信息'); ?></a></li>
    </ul>
    <form class="form-horizontal" name="tag_form" id="tag_form" method="post" action="">
        <input type="hidden" name="goods_code" id="goods_code" value="<?php echo $this->goods['code'] ?>">
        <div class="tab-content">
            <div id="sticky_navigation" style="height:30px;border-bottom:4px solid #DFDFDF;">
                <div class="pull-left" style="font-size:16px;line-height:30px;"><strong><?php echo $this->translate('商品设置'); ?></strong></div>
                <div class="pull-right">
                    <a href="<?php echo $this->url('dbapi/default',array('action'=>'goodsIndex')); ?>" class="btn btn-small"><i class="icon-arrow-left"></i> <?php echo $this->translate('返回商品设置列表'); ?></a>&nbsp;
                    <a href="<?php echo $this->serverUrl(true); ?>" class="btn btn-small btn-warning"><i class="icon-repeat icon-white"></i> <?php echo $this->translate('刷新'); ?></a>&nbsp;
                </div>
            </div>
            <div id="tag_a" class="tab-pane active">
                <div class="well admin_add_header_well"><?php echo $this->translate('基本信息'); ?></div>
                <div class="well admin_add_well">
                    <div class="message_one">
                    </div>
                        <div class="control-group">
                            <input type="text" placeholder="<?php echo $this->translate('请输入需要添加的商品部分或者全部名称'); ?>" id="tag_goods_keyword" name="tag_goods_keyword"  value="" class="span5"><input type="hidden" name="tag_goods_id" id="tag_goods_id" value="" />&nbsp;&nbsp;<button type="button" onclick="add_api_goods_js();" class="btn btn-small btn-primary"><i class="icon-plus icon-white"></i> <?php echo $this->translate('点击添加API调用商品'); ?></button>
                            <i class="cus-help shop_admin_help_note" data-content="<?php echo $this->translate('在输入框中输入商品部分或者全部名称，然后点击添加按钮，即可添加API商品'); ?>"></i>
                        </div>
                    <table class="table table-hover" id="list_goods_show">

                        <thead style="background: #CCC;">
                        <tr>
                            <th width="7%"><?php echo $this->translate('排序'); ?></th>
                            <th width="7%">ID</th>
                            <th width="30%"><?php echo $this->translate('商品名称'); ?></th>
                            <th width="9%"><?php echo $this->translate('商品货号'); ?></th>
                            <th width="10%"><?php echo $this->translate('商品价格'); ?></th>
                            <th width="7%"><?php echo $this->translate('状态'); ?></th>
                            <th width="13%"><?php echo $this->translate('操作'); ?></th>
                        </tr>
                        </thead>
                        <?php
                        if ($this->goods_list) {
                            foreach ($this->goods_list as $goods_value) {
                                ?>
                                <tr id="tag_goods_<?php echo $goods_value['goods_id']; ?>">
                                    <td><input type="text" name="dbapi_goods_id[<?php echo $goods_value['dbapi_goods_id']; ?>]" id="dbapi_goods_id_<?php echo $goods_value['dbapi_goods_id']; ?>" class="span12" value="<?php echo $goods_value['dbapi_goods_sort']; ?>" /></td>
                                    <td><?php echo $goods_value['goods_id']; ?></td>
                                    <td><?php echo $this->escapeHtml($goods_value['goods_name']); ?></td>
                                    <td><?php echo $this->escapeHtml($goods_value['goods_item']); ?></td>
                                    <td><?php echo $goods_value['goods_shop_price']; ?></td>
                                    <td><?php echo (intval($goods_value['goods_state']) == 2 ? $this->translate('下架') : ($goods_value['goods_state']== 1 ? $this->translate('上架') : '')); ?></td>
                                    <td>
                                        <a href="javascript:;" onclick="return update_goods_js(<?php echo $goods_value['dbapi_goods_id']; ?>);"><?php echo $this->translate('点击更新'); ?></a>
                                        <span>|</span>
                                        <a href="javascript:;" onclick="return del_goods_js('<?php echo $this->goods['code'] ?>', <?php echo $goods_value['dbapi_goods_id']; ?>);"><?php echo $this->translate('删除'); ?></a>
                                    </td>
                                </tr>
                                <?php
                            }
                        }
                        ?>
                    </table>

                </div>
            </div>

        </div>
    </form>
</div>
<link href="<?php echo $this->basePath('public/js/jquery-autocomplete/jquery.autocomplete.css'); ?>" rel="stylesheet">
<script src="<?php echo $this->basePath('public/js/jquery-autocomplete/jquery.autocomplete.min.js'); ?>"></script>
<script>

    //jquery-autocomplete查询商品
    $("#tag_goods_keyword").autocomplete("<?php echo $this->url('goods/default',array('action'=>'autocompleteGoodsSearch')); ?>", {
        //mustMatch: true,
        max: 20,
        minChars:1,
        scrollHeight:400
    });
    $("#tag_goods_keyword").result(function(event, data, formatted) {
        if (data) $("#tag_goods_id").val(data[1]);
    });
    //添加api商品
    function add_api_goods_js() {
        var goods_id = $('#tag_goods_id').val();
        var goods_code = $('#goods_code').val();
        if(goods_id == '') {
            alert('<?php echo $this->translate('该商品不存在，请从新选择！'); ?>');
            return false;
        }
        $.post("<?php echo $this->url('dbapi/default',array('action'=>'addApiGoods')); ?>",{goods_code:goods_code,goods_id:goods_id},
            function(data){
                if(data.state == 'true') {
                    window.location.reload();
                } else if(data.state == 'have') {
                    alert('<?php echo $this->translate('该商品已经存在，无需再次添加！'); ?>');
                } else {
                    alert('<?php echo $this->translate('商品添加失败！'); ?>');
                }
            },'json');
    }
    function del_goods_js(goods_code, dbapi_goods_id) {
        if(window.confirm('<?php echo $this->translate('您确实要删除该API商品吗？'); ?>')) {
            $.post("<?php echo $this->url('dbapi/default',array('action'=>'delApiGoods')); ?>",{goods_code:goods_code,dbapi_goods_id:dbapi_goods_id},
                function(data){
                    if(data != 'false') {
                        window.location.reload();
                        return true;
                    } else {
                        alert('<?php echo $this->translate('API商品删除失败！'); ?>');
                    }
                });
        }
        return false;
    }
    function update_goods_js(dbapi_goods_id) {
        var dbapi_goods_sort = $('#dbapi_goods_id_'+dbapi_goods_id).val();
        $.post("<?php echo $this->url('dbapi/default',array('action'=>'updateApiGoods')); ?>",{dbapi_goods_sort:dbapi_goods_sort,dbapi_goods_id:dbapi_goods_id},
            function(data){
                if(data != 'false') {
                    window.location.reload();
                    return true;
                } else {
                    alert('<?php echo $this->translate('API商品更新失败！'); ?>');
                }
            });
    }
</script>